import { defineStore } from 'pinia'
import { ref } from 'vue'
import { generateVideos, getTaskStatus, getTaskList, generateAiVideos } from '@/api/task'
import { ElMessage } from 'element-plus'

export const useTaskStore = defineStore('task', () => {
    const tasks = ref([])
    const loading = ref(false)

    // 生成视频
    const generateBatchVideos = async (params) => {
        try {
            loading.value = true
            const { data } = await generateVideos(params)
            ElMessage.success('视频生成任务已提交')
            return data
        } catch (error) {
            ElMessage.error(error.message || '提交视频生成任务失败')
            throw error
        } finally {
            loading.value = false
        }
    }

    // 多数字人模式生成视频
    const generateBatchAiVideos = async (params) => {
        try {
            loading.value = true
            const { data } = await generateAiVideos(params)
            ElMessage.success('视频生成任务已提交')
            return data
        } catch (error) {

        } finally {
            loading.value = false
        }
    }

    // 获取任务
    const fetchTaskList = async (params) => {
        try {
            const { data } = await getTaskList(params)
            tasks.value = data
        } catch (error) {
            ElMessage.error(error.message || '获取任务状态失败')
        }
    }

    // 获取任务状态
    const fetchTaskStatus = async (taskIds) => {
        try {
            const { data } = await getTaskStatus(taskIds)
            tasks.value = data
        } catch (error) {
            ElMessage.error(error.message || '获取任务状态失败')
        }
    }

    return {
        tasks,
        loading,
        generateBatchVideos,
        fetchTaskStatus,
        fetchTaskList,
        generateBatchAiVideos
    }
})